<?php

/*
 * Snippet 2 from
 * http://www.php.net/manual/en/language.types.boolean.php
 */


/*
 * Create an instance of IndentedTextWriter to hold the generated code
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the code generator options class
 */

$options = new CodeGeneratorOptions();

/*
 * Set the bracing style:
 */

$options->set_BracingStyle(BracingStyle::KR);

/*
 * both $writer and $options will be arguments to one of
 * the PHPCodeProvider code generating methods.
 * Create an instance of the PHP code provider class
 */

$provider = new PHPCodeProvider();

/*
 * Start creating the code tree.
 */

$defaultNamespace = new CodeNamespace();

/*
 * In the snipet the var_dump() function is called several times,
 * so create a reference to it.
 */

$var_dump_ref = new FunctionReferenceExpression('var_dump');

/*
 * Create the function calls
 */

$var_dump_invoke1 = new FunctionInvokeExpression($var_dump_ref);
$var_dump_invoke1->Parameters->add(
    new CastExpression(
        CastType::BOOL, 
        new PrimitiveExpression('')));
$var_dump_statement1 = new ExpressionStatement($var_dump_invoke1);

$var_dump_invoke2 = new FunctionInvokeExpression($var_dump_ref);
$var_dump_invoke2->Parameters->add(
    new CastExpression(
        CastType::BOOL, 
        new PrimitiveExpression(1)));
$var_dump_statement2 = new ExpressionStatement($var_dump_invoke2);

$var_dump_invoke3 = new FunctionInvokeExpression($var_dump_ref);
$var_dump_invoke3->Parameters->add(
    new CastExpression(
        CastType::BOOL, 
        new PrimitiveExpression(-2)));
$var_dump_statement3 = new ExpressionStatement($var_dump_invoke3);

$var_dump_invoke4 = new FunctionInvokeExpression($var_dump_ref);
$var_dump_invoke4->Parameters->add(
    new CastExpression(
        CastType::BOOL, 
        new PrimitiveExpression('foo')));
$var_dump_statement4 = new ExpressionStatement($var_dump_invoke4);

$var_dump_invoke5 = new FunctionInvokeExpression($var_dump_ref);
$var_dump_invoke5->Parameters->add(
    new CastExpression(
        CastType::BOOL, 
        new PrimitiveExpression(2.3e5)));
$var_dump_statement5 = new ExpressionStatement($var_dump_invoke5);

$var_dump_invoke6 = new FunctionInvokeExpression($var_dump_ref);
$array_create_expr = new ArrayCreateExpression();
$array_create_expr->Initializers->add(new PrimitiveExpression(12));
$var_dump_invoke6->Parameters->add(
    new CastExpression(CastType::BOOL, $array_create_expr));
$var_dump_statement6 = new ExpressionStatement($var_dump_invoke6);

$var_dump_invoke7 = new FunctionInvokeExpression($var_dump_ref);
$array_create_expr1 = new ArrayCreateExpression();
$var_dump_invoke7->Parameters->add(
    new CastExpression(CastType::BOOL, $array_create_expr1));
$var_dump_statement7 = new ExpressionStatement($var_dump_invoke7);

$var_dump_invoke8 = new FunctionInvokeExpression($var_dump_ref);
$var_dump_invoke8->Parameters->add(
    new CastExpression(
        CastType::BOOL, 
        new PrimitiveExpression("false")));
$var_dump_statement8 = new ExpressionStatement($var_dump_invoke8);

/*
 * Add the statements to the default namespace
 */

$defaultNamespace->Statements->add($var_dump_statement1);
$defaultNamespace->Statements->add($var_dump_statement2);
$defaultNamespace->Statements->add($var_dump_statement3);
$defaultNamespace->Statements->add($var_dump_statement4);
$defaultNamespace->Statements->add($var_dump_statement5);
$defaultNamespace->Statements->add($var_dump_statement6);
$defaultNamespace->Statements->add($var_dump_statement7);
$defaultNamespace->Statements->add($var_dump_statement8);

/*
 * Generate the code
 */

$provider->generateCodeFromNamespace($defaultNamespace, $writer, $options);

/*
 * Print the generated code
 */

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

/*
 * The output should be
 * 
 * var_dump(((bool)('')));
 * var_dump(((bool)(1)));
 * var_dump(((bool)(-2)));
 * var_dump(((bool)('foo')));
 * var_dump(((bool)(230000)));
 * var_dump(((bool)(array(12))));
 * var_dump(((bool)(array())));
 * var_dump(((bool)('false')));
 * 
 */

?>
